package com.whsxt.utils;

import cn.hutool.core.collection.CollectionUtil;
import com.whsxt.domain.Customer;
import com.whsxt.domain.Permission;
import com.whsxt.domain.Role;
import com.whsxt.vo.BaseDtreeVo;
import com.whsxt.vo.DropDownVo;
import com.whsxt.vo.MenuVo;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 将查询出来的菜单数据转化为页面显示的JSON数据
 */
public class Result2DTreeVoUtils {

    public static List<MenuVo> perms2MenuPlus(List<Permission> permissions){
        /**
         * 判断查询的菜单数据是否为空
         */
        if(CollectionUtils.isEmpty(permissions)){
            return Collections.emptyList();
        }

        // 获取一个Map集合
        Map<Integer, MenuVo> mappings = new LinkedHashMap<>();
        // 将菜单的 id作为key, 自身作为value
        for (Permission permission : permissions) {
            mappings.put(permission.getId(), perms2MenuVo(permission));
        }
        // 定义菜单的根节点
        List<MenuVo> roots = new ArrayList<>();

        // 遍历Map集合
        mappings.forEach((id, menuVo)->{
            // 获取父元素的id
            Integer parentId = menuVo.getParentId();
            // 获取父元素的值
            MenuVo parent = mappings.get(parentId);
            if(parent != null){
                parent.getChildren().add(menuVo);
            } else {
                // 父亲不存在，就为根节点
                roots.add(menuVo);
            }
        });

        return roots;
    }

    /**
     * 将权限对象转化为菜单视图对象
     * @param permission
     * @return
     */
    private static MenuVo perms2MenuVo(Permission permission) {
        MenuVo menuVo = new MenuVo();
        BeanUtils.copyProperties(permission, menuVo);
        menuVo.setOpenType(permission.getType().equals(0) ? null : "_iframe");
        return menuVo;
    }

    /**
     * 将觉数据对象转化为 BaseDtreeVo对象
     * @param role
     * @return
     */
    private static BaseDtreeVo role2BaseDtreeVo(Role role) {

        BaseDtreeVo baseDtreeVo = new BaseDtreeVo();
        baseDtreeVo.setId(role.getId());
        baseDtreeVo.setParentId(0);
        baseDtreeVo.setTitle(role.getName());
        return baseDtreeVo;
    }

    /**
     * 将权限数据对象转化为BaseDtreeVo对象
     * @param permission
     * @return
     */
    private static BaseDtreeVo perm2BaseDtreeVo(Permission permission) {
        BaseDtreeVo baseDtreeVo = new BaseDtreeVo();
        BeanUtils.copyProperties(permission, baseDtreeVo);
        return baseDtreeVo;
    }

    /**
     * 将cunstomer类转化为DropDownVo对象
     * @param customer
     * @return
     */
    private static DropDownVo customer2DropDownVo(Customer customer) {
        DropDownVo dropDownVo = new DropDownVo();
        dropDownVo.setId(customer.getId());
        dropDownVo.setTitle(customer.getName());
        return dropDownVo;
    }

    /**
     * 大处着手，小处着眼
     * 将角色转化为角色的dtree数据
     * @param roles
     * @param userRoleIds
     * @return
     */
    public static List<BaseDtreeVo> roles2Dtree(List<Role> roles, List<Integer> userRoleIds) {
        // 先判断角色数据是否为空
        if(CollectionUtils.isEmpty(roles)){
            return Collections.emptyList();
        }
        // 给定长度，防止扩容(优化)
        List<BaseDtreeVo> baseDtreeVos = new ArrayList<>(roles.size());

        for (Role role : roles) {
            BaseDtreeVo dtreeVo = role2BaseDtreeVo(role);
            if(userRoleIds.contains(dtreeVo.getId())){
                dtreeVo.setCheckArr("1");
            } else {
                dtreeVo.setCheckArr("0");
            }
            baseDtreeVos.add(dtreeVo);
        }
        return baseDtreeVos;
    }

    /**
     * 将权限数据集合转化为 BaseDtreeVo对象集合
     * @param permissions
     * @param permsIds
     * @return
     */
    public static List<BaseDtreeVo> perms2Dtree(List<Permission> permissions, List<Integer> permsIds) {
        // 首先判断权限列表是否为空
        if(CollectionUtils.isEmpty(permissions)){
            return Collections.emptyList();
        }

        List<BaseDtreeVo> baseDtreeVos = new ArrayList<>();

        for (Permission permission : permissions) {
            BaseDtreeVo baseDtreeVo = perm2BaseDtreeVo(permission);
            if(permsIds.contains(baseDtreeVo.getId())){
                baseDtreeVo.setCheckArr("1");
            } else {
                baseDtreeVo.setCheckArr("0");
            }
            baseDtreeVos.add(baseDtreeVo);
        }

        return baseDtreeVos;
    }

    /**
     * 将cusntomer集合对象
     * 转化未DropDownVo集合对象
     * @param customerList
     * @return
     */
    public static List<DropDownVo> customerList2DropDownVoList(List<Customer> customerList) {
        if(CollectionUtil.isEmpty(customerList)){
            return Collections.emptyList();
        }

        List<DropDownVo> dropDownVos = new ArrayList<>(customerList.size());
        for (Customer customer : customerList) {
            DropDownVo dropDownVo = customer2DropDownVo(customer);
            dropDownVos.add(dropDownVo);
        }

        return dropDownVos;
    }


}
